package com.global.college.adapter;

import android.content.Intent;
import android.os.Handler;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.global.college.R;
import com.global.college.data.net.TrainStageBean;
import com.global.college.ui.activity.CourseDetailActivity;
import com.global.college.utils.StringUtils;
import com.global.college.utils.ToastUtils;
import com.global.college.views.RoundCornerImageview;

import java.util.List;

/**
 * @author: xjh
 * @datetime: 2021/12/7
 */
public class TrainExpandAdapter extends BaseMultiItemQuickAdapter<MultiItemEntity, BaseViewHolder> {
    private static String TAG = "TrainExpandAdapter";
    public static final int TYPE_TRAIN_0 = 0;
    public static final int TYPE_TRAIN_1 = 1;
    public static final int TYPE_TRAIN_COURSE = 2;

    private String trainId;

    private boolean isFirstLoad = true;


    public TrainExpandAdapter(List data, String tid) {
        super(data);
        trainId = tid;
        addItemType(TYPE_TRAIN_0, R.layout.item_train_first);
        addItemType(TYPE_TRAIN_1, R.layout.item_train_second);
        addItemType(TYPE_TRAIN_COURSE, R.layout.item_train_course);
    }

    @Override
    protected void convert(BaseViewHolder holder, MultiItemEntity item) {
        switch (holder.getItemViewType()) {
            case TYPE_TRAIN_0:
                if (isFirstLoad) {
                    new Handler().postDelayed(() -> expand(holder.getAdapterPosition()), 50);//毫秒
                }
                TrainStageBean item0 = (TrainStageBean) item;
                Log.i(TAG, "第一级点击：" + item0.getTrainingClassList().size() + "," + item0.getClassModelList().size());
                holder.setText(R.id.tv_stage_name, item0.getName());
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
//                        int pos = holder.getAdapterPosition();
//                        if (item0.isExpanded()) {
//                            collapse(pos);
//                        } else {
//                            expand(pos);
//                        }
                    }
                });
                break;
            case TYPE_TRAIN_1:
                TrainStageBean.TrainWeekBean item1 = ((TrainStageBean.TrainWeekBean) item);
                holder.setText(R.id.tv_week_name, item1.getName());
                ImageView ivExpand = holder.getView(R.id.iv_expand);
                if (item1.isExpanded()) {
                    ivExpand.setImageResource(R.mipmap.icon_course_expand_up);
                } else {
                    ivExpand.setImageResource(R.mipmap.icon_course_expand_down);
                    if (isFirstLoad){
                        new Handler().postDelayed(() -> expand(holder.getAdapterPosition()), 50);//毫秒
                        new Handler().postDelayed(() -> isFirstLoad =false, 100);//毫秒
                    }
                }
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        isFirstLoad = false;
                        int pos = holder.getAdapterPosition();
                        if (item1.isExpanded()) {
                            collapse(pos);
                        } else {
                            expand(pos);
                        }
                    }
                });
                break;
            case TYPE_TRAIN_COURSE:
                TrainStageBean.TrainWeekBean.ClassModelListBean item2 = (TrainStageBean.TrainWeekBean.ClassModelListBean) item;
                RoundCornerImageview riv = holder.getView(R.id.riv_class_pic);
                Glide.with(mContext).load(item2.getClassCover()).into(riv);
                holder.setText(R.id.tv_class_name, item2.getClassName());
                if (!StringUtils.isEmpty(item2.getClassNote())){
                    holder.setText(R.id.tv_class_info, Html.fromHtml(item2.getClassNote()));
                }
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (item2.getClassDuration() == 0){
                            ToastUtils.showCenterToast("暂未上线，敬请期待！");
                            return;
                        }
                        mContext.startActivity(new Intent(mContext, CourseDetailActivity.class)
                                .putExtra("classId", item2.getClassId())
                                .putExtra("trainId", trainId));
                    }
                });
                break;
            default:
                break;
        }
    }
}
